package com.wenwu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsEntity;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsEntityMapper;
import com.wenwu.service.OtherEntityService;

@Service
public class OtherEntityServiceImpl implements OtherEntityService {

	@Autowired
	private RelicsEntityMapper relicsEntityMapper;
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;
	
	public String addObj(RelicsEntity relicsEntity)throws RelicsException{
		try{
			String result = null;
			int num = 0 ;
			List<RelicsAttachment> list = relicsEntity.getFileList();
			if (relicsEntity.getEntityId() == null) {
				num += relicsEntityMapper.insertSelective(relicsEntity);
				if (relicsEntity.getEntityId() > 0) {
					if (list != null && list.size() > 0) {
						for (RelicsAttachment attachment : list) {
							attachment.setAttachmentDesc("其他实物资料附件");
							attachment.setTableOwner(relicsEntity.getEntityId());// 外键
						}
						num += attachmentMapper.insertList(list);
					}
				}
				if (num > 0) {
					result = "其他实物资料添加成功";
				} else {
					result = "其他实物资料添加失败";
				}
			} else {
				num += attachmentMapper.deleteByKey(relicsEntity.getEntityId(),relicsEntity.getOwnList());//先删除原有的附件，再添加
				if (list != null && list.size() > 0) {
					for (RelicsAttachment attachment : list) {
						attachment.setAttachmentDesc("其他实物资料附件");
						attachment.setTableOwner(relicsEntity.getEntityId());// 外键
					}
					num += attachmentMapper.insertList(list);
				}
				num += relicsEntityMapper.updateByPrimaryKeySelective(relicsEntity);
				if (num > 0) {
					result = "其他实物资料修改成功";
				} else {
					result = "其他实物资料修改失败";
				}
			}
			return result;
		}catch(Exception e){
			throw new RelicsException("系统错误，请稍后再试",e);
		}
	}

	public RelicsEntity selectBySelftKey(Integer key)throws RelicsException {
		try{
			return relicsEntityMapper.selectByPrimaryKey(key);
		}catch(Exception e){
			throw new RelicsException("系统错误，请稍后再试",e);
		}
	}

	public String deleteBykeys(String keys) throws RelicsException {
		String result = "删除失败！";
		if (keys != null) {
			try {
				List<String> keyList = new ArrayList<String>();
				String[] objs = keys.split(",");
				for (String k : objs) {
					keyList.add(k);
				}
				result = "成功删除" + relicsEntityMapper.deleteInfoByKey(keyList) + "条数据";
			} catch (Exception e) {
				throw new RelicsException("系统错误，请稍后再试", e);
			}
		}
		return result;
	}

	public PaginationVO selectByCondition(RelicsEntity relicsEntity,
			PaginationVO pageVo) throws RelicsException {
		try {
			List<RelicsEntity> list = relicsEntityMapper.selectListByPage(relicsEntity, pageVo);
			int count = relicsEntityMapper.selectTotalRecord(relicsEntity);
			pageVo.setList(list);
			pageVo.setTotalRecord(count);
			return pageVo;
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

}
